<?php
class uploadController extends accessController{
    public static function _right(){
        return array(
            array('附件管理',1),
            array('图片上传','add,add_'),
            array('附件上传','add2,add2_'),
            array('附件列表','mylist'),
            array('附件删除','del'),
            array('html5巨型附件上传','html5,html5_,html5_sess_'),
        );
    }
	public function s(){
		return array('upload');
	}
	public function add(){
		$callback=$_GET['callback'];
		include($this->viewer_dir.'upload/add.php');
	}
	//传图片
	public function add_(){
		if($_POST){
			$callback=$_POST['callback'];
			$info=$this->s_0->add_();//传图
			
			if($info['state']=='SUCCESS'){
				//file_put_contents(ROOT.'1.txt',json_encode($info));
				$is_thumb=intval($_POST['is_thumb']);
				$width=intval($_POST['width']);
				$height=intval($_POST['height']);
				$types=$this->conf->ADMIN_UPLOAD['image']['allowFiles'];
				if($width>0 and $height>0 and $is_thumb==1 and in_array($info['type'],$types)){
					//生成缩略图
					$file=$this->s_0->disk_path($info['url_base'],'image');
					$dir=dirname($file);
					$image=new Image($dir);
					$new_name=$image->thumb($info['name'],$width,$height,'');
					$new_file=$dir.'/'.$new_name;
					//alert($new_name);
					//更新文件信息
					if(file_exists($new_file)){
						alert('缩略图生成成功！');
						$this->s_0->update_log_info(array('filesize'=>filesize($new_file)),$info['insert_id']);
						$info['url']=pathinfo($info['url'],PATHINFO_DIRNAME).'/'.$new_name;
					}else{
						alert('缩略图生成失败！');
					}
				}
				if(!empty($callback)){
					//url 已经是全路径
					js("window.parent.{$callback}('{$info['url']}');");
					js("parent.$.fn.colorbox.close();");
					exit;
				}else{
					alert('上传成功！');
					back();
				}
			}else{
				alert('上传失败！');
				back();
			}
			
		
		}
	}
	public function add2(){
		$callback=$_GET['callback'];
		include($this->viewer_dir.'upload/add2.php');
	}
	//传附件
	public function add2_(){
		if($_POST){
			$callback=$_POST['callback'];
			$info=$this->s_0->add2_();
			
			
			if($info['state']=='SUCCESS'){
				
				$types=$this->conf->ADMIN_UPLOAD['file']['allowFiles'];
				
				if(!empty($callback)){
					//url 已经是全路径
					js("window.parent.{$callback}('{$info['url']}');");
					js("parent.$.fn.colorbox.close();");
					exit;
				}else{
					alert('上传成功！');
					back();
				}
			}else{
				alert('上传失败！');
				back();
			}
			
		
		}
	}
	public function mylist(){
		$return=$this->s_0->mylist();
		include($this->viewer_dir.'upload/mylist.php');
	}
	
	public function del(){
		$id=intval($_GET['id']);
		if($id>0){
			$r=$this->s_0->del($id);
			autoalert($r);
			back();
		}
	}
	public function html5(){
		$callback=$_GET['callback'];
		include($this->viewer_dir.'upload/html5.php');
	}
	public function html5_sess_(){
		$kind=intval($_GET['kind']);
		if($kind==1){
			$_SESSION['html5_sess']=array();
		}else if($kind==2){
			
			$url=$this->conf->ADMIN_UPLOAD['file']['httpPath'].$_SESSION['html5_sess']['info']['url'];
			
			unset($_SESSION['html5_sess']);
			
			echo $url;
		}
		
	}
	public function html5_(){
		if($_POST){
		
			$callback=$_POST['callback'];
			$file_type=$_POST['file_type'];
			
			if(!$_SESSION['html5_sess']){
				$_SESSION['html5_sess']['file_name']=$file_name=time().rand(1,10000);
			}else{
				$file_name=$_SESSION['html5_sess']['file_name'];
			}
			$info=$this->s_0->html5_(array('file_name'=>$file_name,'file_type'=>$file_type));
			//file_put_contents(ROOT.'1.txt',json_encode($info));
			$_SESSION['html5_sess']['info']=$info;
		}
	}
//
}